package cn.yunhe.validator;

import org.springframework.validation.Errors;
import org.springframework.validation.Validator;

public class OrderValidator implements Validator {
    @Override
    public boolean supports(Class<?> ifOrderClass) {
        //判断验证是否是Order,是则验证(验证的筛选条件,入口Entry)
        return Order.class.equals(ifOrderClass);
    }

    @Override
    public void validate(Object target, Errors errors) {
        Order order2Target= (Order) target;
        //求交易金额和价格X数量的差额
        double diff= order2Target.getAmount() -
                order2Target.getPrice()*order2Target.getQuantity();
        //如果差额大于0.01，认为订单信息错误
        if (diff>10.00){
            errors.rejectValue("amount",null,"交易金额!=单价*总数量,存在误差值");
        }

    }
}
